<?php
$pageTitle = '男友请求';
require_once '../../config/config.php';
require_once '../../includes/functions.php';
require_once '../../classes/User.php';
require_once '../../classes/Friend.php';

// 检查用户是否登录
if (!isLoggedIn()) {
    setFlashMessage('error', '请先登录');
    redirect(SITE_URL . '/login.php');
}

$userId = $_SESSION['user_id'];
$friend = new Friend();
$friendRequests = $friend->getFriendRequests($userId);
$sentRequests = $friend->getSentFriendRequests($userId);

include '../../includes/header.php';
?>

<div class="container py-4">
    <div class="row">
        <div class="col-lg-8 mx-auto">
            <div class="card shadow-sm mb-4">
                <div class="card-header d-flex justify-content-between align-items-center bg-primary text-white">
                    <h5 class="mb-0"><i class="fas fa-user-plus"></i> 收到的男友请求</h5>
                </div>
                <div class="card-body">
                    <?php if (isset($_SESSION['flash_message'])): ?>
                        <div class="alert alert-<?php echo $_SESSION['flash_type']; ?> alert-dismissible fade show">
                            <?php echo $_SESSION['flash_message']; ?>
                            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                        </div>
                        <?php unset($_SESSION['flash_message'], $_SESSION['flash_type']); ?>
                    <?php endif; ?>
                    
                    <?php if (empty($friendRequests)): ?>
                        <div class="text-center py-4">
                            <i class="fas fa-user-check fa-3x text-muted mb-3"></i>
                            <p class="lead">您没有收到新的男友请求</p>
                        </div>
                    <?php else: ?>
                        <div class="list-group">
                            <?php foreach ($friendRequests as $request): ?>
                                <div class="list-group-item list-group-item-action">
                                    <div class="d-flex w-100 justify-content-between align-items-center">
                                        <div class="d-flex align-items-center">
                                            <img src="<?php echo getUserAvatar($request['avatar']); ?>" class="rounded-circle me-3" width="50" height="50" alt="<?php echo htmlspecialchars($request['nickname'] ?: $request['username']); ?>">
                                            <div>
                                                <h6 class="mb-1"><?php echo htmlspecialchars($request['nickname'] ?: $request['username']); ?></h6>
                                                <small class="text-muted">请求时间: <?php echo formatDateTime($request['created_at']); ?></small>
                                            </div>
                                        </div>
                                        <div class="btn-group btn-group-sm">
                                            <form method="POST" action="<?php echo SITE_URL; ?>/api/friend/accept.php" class="d-inline">
                                                <input type="hidden" name="request_id" value="<?php echo $request['id']; ?>">
                                                <button type="submit" class="btn btn-success">
                                                    <i class="fas fa-check"></i> 接受
                                                </button>
                                            </form>
                                            <form method="POST" action="<?php echo SITE_URL; ?>/api/friend/reject.php" class="d-inline">
                                                <input type="hidden" name="request_id" value="<?php echo $request['id']; ?>">
                                                <button type="submit" class="btn btn-danger">
                                                    <i class="fas fa-times"></i> 拒绝
                                                </button>
                                            </form>
                                        </div>
                                    </div>
                                </div>
                            <?php endforeach; ?>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
            
            <div class="card shadow-sm">
                <div class="card-header bg-primary text-white">
                    <h5 class="mb-0"><i class="fas fa-paper-plane"></i> 已发送的男友请求</h5>
                </div>
                <div class="card-body">
                    <?php if (empty($sentRequests)): ?>
                        <div class="text-center py-4">
                            <i class="fas fa-paper-plane fa-3x text-muted mb-3"></i>
                            <p class="lead">您没有发送中的男友请求</p>
                        </div>
                    <?php else: ?>
                        <div class="list-group">
                            <?php foreach ($sentRequests as $request): ?>
                                <div class="list-group-item list-group-item-action">
                                    <div class="d-flex w-100 justify-content-between align-items-center">
                                        <div class="d-flex align-items-center">
                                            <img src="<?php echo getUserAvatar($request['avatar']); ?>" class="rounded-circle me-3" width="50" height="50" alt="<?php echo htmlspecialchars($request['nickname'] ?: $request['username']); ?>">
                                            <div>
                                                <h6 class="mb-1"><?php echo htmlspecialchars($request['nickname'] ?: $request['username']); ?></h6>
                                                <small class="text-muted">发送时间: <?php echo formatDateTime($request['created_at']); ?></small>
                                            </div>
                                        </div>
                                        <form method="POST" action="<?php echo SITE_URL; ?>/api/friend/cancel.php" class="d-inline">
                                            <input type="hidden" name="request_id" value="<?php echo $request['id']; ?>">
                                            <button type="submit" class="btn btn-sm btn-outline-secondary">
                                                <i class="fas fa-ban"></i> 取消请求
                                            </button>
                                        </form>
                                    </div>
                                </div>
                            <?php endforeach; ?>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>
</div>

<?php include '../../includes/footer.php'; ?>
